package com.example.day14.annotation;

import java.lang.annotation.*;
import java.lang.reflect.Method;

//注解的使用
@Inherited //查看当前对象是否会被继承
@Target({ElementType.TYPE,ElementType.METHOD})          //可以使用的参数类型
@Retention(RetentionPolicy.RUNTIME)       //设置在程序运行时可以通过反射调用创建对象
public @interface Annotations {
    String value();
    int age();



}
@Annotations(value = "admin",age = 20)
class  Myannotatio{

            @Annotations(value = "admin",age=18)
            public  void  test(){
                System.out.println("test。。。。。。。。。。the annotation");
            }
}


class  TestAnnotation{
    public static void main(String[] args) {

        Class<?> c=Myannotatio.class;

        //判断该类是否有annotation注解
        if(c.isAnnotationPresent(Annotations.class)){

            Annotations declaredAnnotation = c.getDeclaredAnnotation(Annotations.class);
            System.out.println(declaredAnnotation.age());
            System.out.println(declaredAnnotation.value());
        }

        //获取方法上面的相关属性
        try {
            Method m=c.getDeclaredMethod("test");

            if(m.isAnnotationPresent(Annotations.class)){
                Annotations declaredAnnotation = m.getDeclaredAnnotation(Annotations.class);
                System.out.println(declaredAnnotation.value());
                System.out.println(declaredAnnotation.age());
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}